package clients;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import javax.swing.JProgressBar;
import javax.swing.JPasswordField;
import javax.swing.JTextPane;
import javax.swing.JFormattedTextField;
import javax.swing.JEditorPane;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JSeparator;

import shared.*;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollBar;
public class ClientFrame extends JFrame {
	
	
	private Client client;
	private JTextField textFieldStockName;
	private JTextField textFieldAmount;
	private JTextArea textArea;
	private JFormattedTextField messageField;
	private JTextField textFieldUserName;
	private JTextField textFieldBalance;
	private JTextArea stockArea;
	/**
	 * Create the frame.
	 */
	public ClientFrame(Client newClient) {
		setTitle("Normal User GUI");
		client = newClient;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 533, 424);
		getContentPane().setLayout(null);
		
		textFieldStockName = new JTextField();
		textFieldStockName.setBounds(376, 46, 91, 23);
		getContentPane().add(textFieldStockName);
		textFieldStockName.setColumns(10);
		
		textFieldAmount = new JTextField();
		textFieldAmount.setBounds(358, 312, 109, 20);
		getContentPane().add(textFieldAmount);
		textFieldAmount.setColumns(10);
		
		JLabel tickerLabel = new JLabel("Stock Ticker Symbol:");
		tickerLabel.setFont(UIManager.getFont("Label.font"));
		tickerLabel.setBounds(244, 53, 138, 16);
		getContentPane().add(tickerLabel);
		
		JLabel lblForAmount = new JLabel("For Amount:");
		lblForAmount.setBounds(254, 314, 86, 16);
		getContentPane().add(lblForAmount);
		
		JPanel panelStock = new JPanel();
		panelStock.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "Stock Info", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panelStock.setBounds(253, 107, 214, 200);
		getContentPane().add(panelStock);
		panelStock.setLayout(null);
		
		this.textArea = new JTextArea();
		textArea.setFont(UIManager.getFont("ScrollPane.font"));
		textArea.setBounds(5, 18, 198, 141);
		panelStock.add(textArea);
		textArea.setEditable(false);
		
		messageField = new JFormattedTextField();
		messageField.setBounds(5, 162, 198, 38);
		panelStock.add(messageField);
		messageField.setFont(new Font("Candara", Font.PLAIN, 12));
		messageField.setEditable(false);
		
		JButton btnBuy = new JButton("Buy Stock");
		btnBuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String stockName = textFieldStockName.getText().toUpperCase();
				if (stockName.length() == 0){
					JOptionPane.showMessageDialog(null, "Please enter a stock ticker symbol");
					return;
				}
				
				Stock stock = client.queryStock(stockName);
				
				if (stock ==null){
					JOptionPane.showMessageDialog(null, "Sorry, the ticker sysmbol you input is not found");
					return;
				}
				textArea.setText(stock.toString());
				int amount=0;
				try {
					amount = Integer.parseInt(textFieldAmount.getText());
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Please input a valid amount");
				}
				Status status = new Status();
				status = client.buy(stockName, amount);
				messageField.setText(status.getMessage());
				textFieldBalance.setText(String.format("%.2f", client.getCurrentUser().getBalance()));
				stockArea.setText(client.getCurrentUser().toString());
				
				stock = client.queryStock(stockName);
				textArea.setText(stock.toString());

			}
		});
		
		JButton btnSell = new JButton("Sell Stock");
		btnSell.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String stockName = textFieldStockName.getText().toUpperCase();
				stockName.toUpperCase();
				if (stockName.length() == 0){
					JOptionPane.showMessageDialog(null, "Please enter a stock ticker symbol");
					return;
				}
				
				Stock stock = client.queryStock(stockName);
				if (stock ==null){
					JOptionPane.showMessageDialog(null, "Sorry, the ticker sysmbol you input is not found");
					return;
				}
				int amount=0;
				textArea.setText(stock.toString());
				try {
					amount = Integer.parseInt(textFieldAmount.getText());
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "Please input a valid amount");
				}
				Status status = new Status();
				
				status = client.sell(stockName, amount);
				messageField.setText(status.getMessage());
				textFieldBalance.setText(String.format("%.2f", client.getCurrentUser().getBalance()));
				stockArea.setText(client.getCurrentUser().toString());
				stock = client.queryStock(stockName);
				textArea.setText(stock.toString());
			}
		});
		
		JButton btnQuery = new JButton("Query Stock");
		btnQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String stockName = textFieldStockName.getText().toUpperCase();
				if (stockName.length() == 0){
					JOptionPane.showMessageDialog(null, "Please enter a stock ticker symbol");
					return;
				}
				
				Stock stock = client.queryStock(stockName);
				if (stock ==null){
					JOptionPane.showMessageDialog(null, "Sorry, the ticker sysmbol you input is not found");
					return;
				}
				
				textArea.setText(stock.toString());
			}
		});
		btnQuery.setBounds(296, 81, 114, 23);
		getContentPane().add(btnQuery);
		btnSell.setBounds(369, 344, 98, 23);
		getContentPane().add(btnSell);
		btnBuy.setBounds(253, 344, 98, 23);
		getContentPane().add(btnBuy);
		
		textFieldUserName = new JTextField();
		textFieldUserName.setEditable(false);
		textFieldUserName.setBounds(12, 77, 106, 20);
		textFieldUserName.setText(client.getCurrentUser().getName());
		getContentPane().add(textFieldUserName);
		textFieldUserName.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("My user name:");
		lblNewLabel.setBounds(12, 50, 114, 23);
		getContentPane().add(lblNewLabel);
		
		textFieldBalance = new JTextField();
		textFieldBalance.setEditable(false);
		textFieldBalance.setBounds(130, 77, 72, 20);
		textFieldBalance.setText(String.format("%.2f", client.getCurrentUser().getBalance()));
		getContentPane().add(textFieldBalance);
		textFieldBalance.setColumns(10);
		
		JLabel lblMyBalance = new JLabel("My balance:");
		lblMyBalance.setBounds(130, 53, 78, 16);
		getContentPane().add(lblMyBalance);
		
		JLabel lblNewLabel_1 = new JLabel("Stock Trading System");
		lblNewLabel_1.setBackground(UIManager.getColor("CheckBox.focus"));
		lblNewLabel_1.setFont(new Font("Lucida Calligraphy", Font.BOLD, 16));
		lblNewLabel_1.setBounds(158, 12, 224, 22);
		getContentPane().add(lblNewLabel_1);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(184, 207, 229)), "My Stock List", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(17, 107, 207, 261);
		getContentPane().add(panel);
		panel.setLayout(null);
		
		stockArea = new JTextArea();
		stockArea.setBounds(5, 18, 197, 256);
		stockArea.setText(client.getCurrentUser().toString());
		panel.add(stockArea);
		
		JScrollBar scrollBar = new JScrollBar();
		scrollBar.setBounds(185, 22, 17, 61);
		panel.add(scrollBar);
		
		

	}
}
